/**
 * @description 根据roles数组拦截路由
 * @param routes 路由
 * @param rolesControl 是否进行权限控制
 * @param baseUrl 基础路由
 * @returns {[]}
 */
import store from '@/store/index'
import deepClone from '@/z/callback/deepClone'
function zhipdfn(zhi, pddata) {
  let r = zhi.values.find(function (e) {
    if (typeof e == 'object') {
      for (let key in e) {
        if (pddata[key] != e[key]) {
          return false
        }
      }
      return true
    } else {
      return pddata.utype == e
    }
  })
  return r || r == '0' ? true : false
}
function routeriffn(route, pddata) {
  if (!route.meta) {
    return false
  }

  // pddata.b_rzfck = 0
  if (Object.keys(pddata).length >= 6) {
    let u = route.meta
    u.if = u.if || []
    if (
      (u.url == './html/StatDftj.html' ||
        u.url == './html/UwcashLog_rz.html') &&
      pddata.utype == 0
    ) {
      u.if = u.if.filter(function (e) {
        return e.type != 'zhi'
      })
      u.if.push({ type: 'bxzhi', values: [{ b_rzfck: 1 }] })
    }

    if (u.url == './html/GetRfCD.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_tjcd: 1 }] })
    } else if (
      u.url == './html/GetRfSim.html' ||
      u.url == './html/ChargeCarllf.html'
    ) {
      u.if.push({ type: 'bxzhi', values: [{ b_llfck: 1 }] })
    } else if (u.url == './html/GetRfCDfy.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_tjcw: 1 }] })
    } else if (u.url == './html/GetRfCZCard.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_tjccz: 1 }] })
    } else if (u.url == './html/GetRfZZ.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_tjzz: 1 }] })
    } else if (u.url == './html/USearch.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_user: 1 }] })
    } else if (
      u.url == './html/Cardsnew.html' ||
      u.url == './html/Cardyhgl.html'
    ) {
      u.if.push({ type: 'bxzhi', values: [{ b_card: 1 }] })
    } else if (u.url == './html/Cardsnewxn.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_card: 1 }, { b_cardxn: 1 }] })
    } else if (u.url == './html/Cardsnewxnadd.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_card: 1 }, { b_cardxn: 1 }] })
    } else if (u.url == './html/GetRfCD.html' || u.url == './html/RecCD.html') {
      u.if.push({ type: 'bxzhi', values: [{ b_tjcd: 1 }] })
    }

    route.meta = u
  }
  let ifdatas = route.meta.if || []
  // url: "./html/ChargeCardt.html"
  if (ifdatas.length == 0) {
    return true
  }

  let bxzhi = ifdatas.filter(function (e) {
    return e.type == 'bxzhi'
  })
  if (bxzhi.length != 0) {
    let r = bxzhi.find(function (e) {
      return !zhipdfn(e, pddata)
    })

    if (r) {
      return false
    }
  }
  let nozhi = ifdatas.filter(function (e) {
    return e.type == 'nozhi'
  })
  if (nozhi.length != 0) {
    let r = nozhi.find(function (e) {
      return zhipdfn(e, pddata)
    })
    if (r) {
      return false
    }
  }
  let zhi = ifdatas.filter(function (e) {
    return e.type == 'zhi'
  })
  if (zhi.length != 0) {
    let r = zhi.find(function (e) {
      return zhipdfn(e, pddata)
    })
    if (!r) {
      return false
    }
  }
  let zhia = ifdatas.filter(function (e) {
    return (
      typeof e == 'string' ||
      typeof e == 'number' ||
      (typeof e == 'object' && !e.type)
    )
  })
  if (zhia.length != 0) {
    if (!zhipdfn({ values: zhia }, pddata)) {
      return false
    }
  }

  return true
}
function hasPermission(e, pddata) {
  let r = routeriffn(e, pddata)
  return r
}
export async function filterRoutes(routes) {
  let USearch = await store.dispatch('user/getUSearch')
  let UPW = await store.dispatch('upw/getUpw')
  // let station = await store.dispatch('station/getstationone', USearch.pcs)

  let pddata = {
    utype: USearch.utype,
    uUser: USearch.uUser,
    udj: USearch.udj || '',
    frid: USearch.frid && USearch.frid != 0 ? true : false,
    ...UPW,
    // b_rza: station[0].b_rza,
  }
  let routesdata = deepClone(routes)

  let r = routesdata
    .filter((route) => hasPermission(route, pddata))
    .map((route) => {
      route = { ...route }
      // route.path =
      //   route.path !== '*' && !isExternal(route.path)
      //     ? resolve(baseUrl, route.path)
      //     : route.path
      let children = []
      let array = route.children || []
      for (let index = 0; index < array.length; index++) {
        const element = array[index]
        if (hasPermission(element, pddata)) {
          children.push(element)
        }
      }
      route.children = children

      return route
    })
    .filter((route) => {
      if (route.children && route.children.length == 0) {
        return false
      }
      return true
    })

  let routers = [...r]
  // map(function (e) {
  //   e.path = `${e.path}/${e.children[0].path}`
  //   return e
  // })
  // console.log(routers, pddata, 99999)
  return routers
}
